package com.santiao.fuel.ui.activity

import android.graphics.Outline
import android.os.Bundle
import android.os.CountDownTimer
import android.text.TextUtils
import android.util.Log
import android.view.View
import android.view.ViewOutlineProvider
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.blankj.utilcode.util.ToastUtils
import com.gyf.immersionbar.ImmersionBar
import com.gyf.immersionbar.ktx.statusBarHeight
import com.santiao.fuel.AppConst
import com.santiao.fuel.R
import com.santiao.fuel.dialog.InputPasswordDialogPopup
import com.santiao.fuel.utils.DeviceUtils
//import com.santiao.fuel.utils.GetHttpDataUtil
import com.santiao.fuel.utils.ScreenUtils
import com.santiao.fuel.utils.UserInfoModel
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.santiao.publics.ui.SettingFragment

class RYUserCenterActivity : AppCompatActivity() {

    private lateinit var topLinearLayout: LinearLayout
    private lateinit var backImageView: ImageView
    private lateinit var user_iv_photo: ImageView
    private lateinit var doLinearlayout: LinearLayout
    private lateinit var privacyLinearLayout: LinearLayout
    private lateinit var protocolLayout: LinearLayout
    private lateinit var user_fk: LinearLayout
    private lateinit var my_tv_version: TextView
    private lateinit var my_tv_dj_number: TextView
    var stat = 0;
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_user)
        ImmersionBar.with(this)
            .transparentStatusBar()
            .transparentNavigationBar()
            .init()
        initView()
        //初始化组件逻辑
        initOnclickSetting()
    }
    private fun initView(){
        //初始化引用
        topLinearLayout=findViewById(R.id.user_top)
        backImageView=findViewById(R.id.user_back)
        doLinearlayout=findViewById(R.id.user_do_lin)
        privacyLinearLayout=findViewById(R.id.user_privacy)
        protocolLayout=findViewById(R.id.user_protocol)
        user_fk=findViewById(R.id.user_fk)
        my_tv_version=findViewById(R.id.my_tv_version)
        my_tv_dj_number=findViewById(R.id.my_tv_dj_number)
        user_iv_photo=findViewById(R.id.user_iv_photo)




        my_tv_version.setText(DeviceUtils.getVersionName(this))
        my_tv_dj_number.setText(UserInfoModel.getDjid())


        privacyLinearLayout.setOnClickListener({
            RYWebViewActivity.forward(
                this@RYUserCenterActivity,
                AppConst.URL_PRIVACY_POLICY,
                getString(R.string.privacy_policy)
            )
        })


        protocolLayout.setOnClickListener({
            RYWebViewActivity.forward(
                this@RYUserCenterActivity,
                AppConst.URL_USER_AGREEMENT,
                getString(R.string.user_agreement)
            )
        })

        user_iv_photo.setOnClickListener({
            stat++;
            Log.i("countDownTimerstat", "${stat}")
            if (stat == 1) {
                countDownTimer.start()
            }
            if (stat > 5) {
                stat = 0
                showInputPasswordDialog()
            }
        })

        user_fk.setOnClickListener({
            RYContactCustomerServiceActivity.Companion.show(this@RYUserCenterActivity);
        })
        //初始化视图
        topLinearLayout.setPadding(ScreenUtils.dip2px(15,this),statusBarHeight,ScreenUtils.dip2px(15,this),0)
        doLinearlayout.outlineProvider=object : ViewOutlineProvider() {
            override fun getOutline(view: View, outline: Outline?) {
                outline?.setRoundRect(0,0,view.width,view.height,ScreenUtils.dip2px(10,this@RYUserCenterActivity).toFloat())
            }
        }
        doLinearlayout.clipToOutline=true


        SettingFragment.adLoad(1, null, null, this, this)
    }
    private fun initOnclickSetting(){
        backImageView.setOnClickListener {
            finish()
        }
    }

    var InputPopupView: BasePopupView? = null
    private fun showInputPasswordDialog() {
        if (InputPopupView?.isShow == true) {
            return
        }
        val customPopup =
            InputPasswordDialogPopup(this)
        customPopup.listener = object : InputPasswordDialogPopup.OnInputPasswordListener {
            override fun cancel() {
            }

            override fun ok(password: String) {
                if (null != password && !TextUtils.isEmpty(password.trim())) {
                    InputPopupView?.dismiss()
//                    GetHttpDataUtil.setWhiteListHttp(password.trim())
                } else {
                    ToastUtils.showLong("密码为空")
                }
            }


        }
        InputPopupView = XPopup.Builder(this)
            .autoOpenSoftInput(false)
            .autoDismiss(false)
//            .enableDrag(false)
            .dismissOnBackPressed(false)
            .dismissOnTouchOutside(false)
            .asCustom(customPopup)
            .show()
    }


    /**
     * CountDownTimer 实现倒计时
     */
    private val countDownTimer: CountDownTimer = object : CountDownTimer(3000, 1000) {
        override fun onTick(millisUntilFinished: Long) {

        }

        override fun onFinish() {
            stat = 0
            Log.i("countDownTimerstat", "${stat}")
        }
    }
}